//using System.Collections;
//using System.Collections.Generic;
//using UnityEngine;

//public class GGNetworkUnityManager : BehaviourSingleton<GGNetworkUnityManager>
//{
//	public delegate void OnMessageReceived(byte[] b);

//	public class DelaySendMsg
//	{
//		public byte[] bytes;

//		public float time;

//		public float delay;
//	}

//	protected static string gameType = "com.giraffegames.realtabletennis";

//	protected float delay;

//	protected float delayRangePercent = 0.25f;

//	private List<DelaySendMsg> delayList = new List<DelaySendMsg>();

//	private NetworkView networkView_;

//	public GGNetworkState state { get; protected set; }

//	protected NetworkView cachedNetworkView
//	{
//		get
//		{
//			if (networkView_ == null)
//			{
//				networkView_ = GetComponent<NetworkView>();
//				if (networkView_ == null)
//				{
//					networkView_ = AddNetworkView();
//				}
//			}
//			return networkView_;
//		}
//	}

//	public event OnMessageReceived onMessage;

//	public void Send(byte[] b)
//	{
//		if (delay > 0f)
//		{
//			delayList.Add(new DelaySendMsg
//			{
//				bytes = b,
//				time = Time.realtimeSinceStartup,
//				delay = delay * (1f + Random.Range(0f - delayRangePercent, delayRangePercent))
//			});
//		}
//		else
//		{
//			cachedNetworkView.RPC("ReceiveRPCMessage", RPCMode.Others, b);
//		}
//	}

//	[RPC]
//	public void ReceiveRPCMessage(byte[] b)
//	{
//		if (this.onMessage != null)
//		{
//			this.onMessage(b);
//		}
//	}

//	private NetworkView AddNetworkView()
//	{
//		NetworkView networkView = base.gameObject.AddComponent<NetworkView>();
//		networkView.observed = null;
//		networkView.stateSynchronization = NetworkStateSynchronization.Off;
//		return networkView;
//	}

//	private void Awake()
//	{
//		networkView_ = AddNetworkView();
//	}

//	public void StartServer(string name)
//	{
//		gameType = name;
//		state = GGNetworkState.NotStarted;
//		if (Network.isClient || Network.isServer)
//		{
//			StartCoroutine(StopThanStartServer());
//		}
//		else
//		{
//			DoStartServer();
//		}
//	}

//	private void DoStartServer()
//	{
//		bool useNat = !Network.HavePublicAddress();
//		int num = 0;
//		int num2 = 3;
//		int num3 = 25000;
//		while (true)
//		{
//			NetworkConnectionError networkConnectionError = Network.InitializeServer(2, num3, useNat);
//			if (networkConnectionError == NetworkConnectionError.CreateSocketOrThreadFailure && num < num2)
//			{
//				num3++;
//				num++;
//				continue;
//			}
//			break;
//		}
//	}

//	private IEnumerator StopThanStartServer()
//	{
//		Time.timeScale = 1f;
//		StopServer();
//		while (Network.isClient || Network.isServer)
//		{
//			yield return null;
//		}
//		DoStartServer();
//	}

//	public void StopServer()
//	{
//		state = GGNetworkState.NotStarted;
//		Network.Disconnect();
//		MasterServer.UnregisterHost();
//	}

//	public void StartClient(string name)
//	{
//		gameType = name;
//		state = GGNetworkState.NotStarted;
//		MasterServer.ClearHostList();
//		state = GGNetworkState.ClientLookingForHosts;
//		GGDebug.DebugLog("Requesting host list");
//		MasterServer.RequestHostList(name);
//	}

//	public void UnregisterHost()
//	{
//		MasterServer.UnregisterHost();
//	}

//	private void OnMasterServerEvent(MasterServerEvent e)
//	{
//		switch (e)
//		{
//		case MasterServerEvent.RegistrationSucceeded:
//			GGDebug.DebugLog("Registration Success");
//			state = GGNetworkState.ServerStarted;
//			break;
//		case MasterServerEvent.RegistrationFailedNoServer:
//			GGDebug.DebugLog("No Server");
//			state = GGNetworkState.Error;
//			break;
//		case MasterServerEvent.RegistrationFailedGameName:
//			GGDebug.DebugLog("Registration Failed Game Name");
//			state = GGNetworkState.ServerErrorNameExists;
//			break;
//		case MasterServerEvent.HostListReceived:
//			GGDebug.DebugLog("Received Hosts");
//			state = GGNetworkState.ReceivedHosts;
//			break;
//		}
//	}

//	private void OnFailedToConnectToMasterServer(NetworkConnectionError error)
//	{
//		GGDebug.DebugLog("Failed to Connect To Master Server");
//		state = GGNetworkState.Error;
//	}

//	private void OnConnectedToServer()
//	{
//		GGDebug.DebugLog("OnConnected to server");
//		state = GGNetworkState.ClientConnected;
//	}

//	private void OnFailedToConnect()
//	{
//		GGDebug.DebugLog("OnFailedToConnect");
//		state = GGNetworkState.Error;
//	}

//	private void OnServerInitialized()
//	{
//		GGDebug.DebugLog("Server Started");
//		state = GGNetworkState.ServerRegistering;
//		MasterServer.RegisterHost(gameType, "Test Server");
//	}

//	private void OnDisconnectedFromServer()
//	{
//		GGDebug.DebugLog("OnDisconected");
//		state = GGNetworkState.NotStarted;
//	}

//	private void OnPlayerConnected()
//	{
//	}

//	private void OnPlayerDisconected()
//	{
//	}

//	public void ConnectClient(HostData hd)
//	{
//		GGDebug.DebugLog("Error " + Network.Connect(hd));
//	}

//	private void Update()
//	{
//		if (state == GGNetworkState.ClientLookingForHosts || state == GGNetworkState.ReceivedHosts)
//		{
//			if (MasterServer.PollHostList().Length > 0)
//			{
//				state = GGNetworkState.ClientFoundHosts;
//				HostData[] array = MasterServer.PollHostList();
//				GGDebug.DebugLog("Host Data available " + array.Length + " 0 " + array[0].gameType + " game type " + gameType);
//				ConnectClient(array[0]);
//				MasterServer.ClearHostList();
//			}
//			else if (state == GGNetworkState.ReceivedHosts)
//			{
//				state = GGNetworkState.HostsListEmpty;
//			}
//		}
//		if (delayList.Count <= 0)
//		{
//			return;
//		}
//		List<DelaySendMsg> list = new List<DelaySendMsg>();
//		foreach (DelaySendMsg delay in delayList)
//		{
//			if (delay.time + delay.delay < Time.realtimeSinceStartup)
//			{
//				list.Add(delay);
//				cachedNetworkView.RPC("ReceiveRPCMessage", RPCMode.Others, delay.bytes);
//			}
//		}
//		foreach (DelaySendMsg item in list)
//		{
//			delayList.Remove(item);
//		}
//		list.Clear();
//	}
//}
